package com.example.mvvmcore.retrofit.impl;

import android.util.Log;


import com.example.mvvmcore.retrofit.IProduct;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class FileProductImpl implements IProduct {

    private Retrofit retrofit;

    @Override
    public Retrofit getRetrofit(String msg) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
//                  .baseUrl("http://82.156.178.182:8087/")
                    .baseUrl(msg)
                    .client(new OkHttpClient.Builder()
                            .connectTimeout(2, TimeUnit.MINUTES)
                            .writeTimeout(2, TimeUnit.MINUTES)
                            .readTimeout(2, TimeUnit.MINUTES)
                            .addInterceptor(new Interceptor() {
                                @Override
                                public Response intercept(Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder().addHeader("Authorization",
                                            "bearer AbQAe0DIIJi1Chy95MvdDaWoQr7D3UKFXKnJ_fxYLmQQPYQB_uc8s83tJ-SY_Rjb820JUzdBo7pm8G6nLgVTFNS7Uety5qsIH_s0IBddcuTtqA4NPoYzIrTUN5tXrYARlSNffH1l71_cFR5FCAELqCATDjqIdfjBw2gB_UtAHhVtX5E9UWlL9Jx9SplCBLNAkbRxZq06KApmmMGBY785cog6gIoMQ0NoX1jQhZYuCXuCMFb0dgJvKP4zmt--B_gdLEpxLaME_EqLbpasZVUIq56G9mRbFZK82rt0bqIoHiA").build();
                                    Response response = chain.proceed(request);
                                    Log.i("------", "intercept: "+response);
                                    return response;
                                }
                            })
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                            .build())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
